package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.ResultInfo;
import domain.User;
import org.apache.commons.beanutils.BeanUtils;
import service.registservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name="registUserServlet",value = "/registUserServlet")
public class registUserServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        String username = req.getParameter("user_name");
        String user_id = req.getParameter("user_id");
        String user_email = req.getParameter("user_email");
        String user_pwd = req.getParameter("user_pwd");
        String user_head = req.getParameter("user_head");
        user.setUsername(username);
        user.setUserid(user_id);
        user.setUseremail(user_email);
        user.setPassword(user_pwd);
        user.setUserhead(user_head);

        //3.service实现注册
        registservice service = new registservice();
        boolean flag = service.regist(user);
        ResultInfo info = new ResultInfo();
        //4.响应结果
        if(flag){
            //注册成功
            info.setFlag(true);
        }else{
            //注册失败
            info.setFlag(false);

        }
        //将 info 对象序列化为 json
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        //将 json 数据写回客户端
        //设置 content-type
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        this.doPost(request, response);
    } }


